﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DoAnASP;

public partial class Admin_QuanLiSanPham : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            load_nhomSP();
            Load_sanpham();
        }
    }

   
    protected void Button1_Click(object sender, EventArgs e)
    {
        //FormView1.ChangeMode(FormViewMode.Insert);
        Response.Redirect("~/Admin/ThemMoiSanPham.aspx");
    }
    protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Admin/QuanLiSanPham.aspx");
    }

    protected void btnLuu_Click(object sender, EventArgs e)
    {
        BUS_SanPham busSP = new BUS_SanPham();       
        DTO_SanPham sp = new DTO_SanPham();
        DTO_ThongTinSP ttsp = new DTO_ThongTinSP();
        sp.ThuongHieu = int.Parse(ddlThuonghieu.SelectedValue);
        sp.TenSanPham = txtTenSP.Text;
        sp.Anh = "duongdan/" + txtHinhAnh.Text;
        sp.Gia = Convert.ToDouble(txtGia.Text);
        sp.TinhTrang = ddlTinhtrang.SelectedValue;
        sp.ThoiGanBH = ddlBaohanh.SelectedValue;
        sp.SoLuong = int.Parse(txtSoluong.Text);
        ttsp.CongngheCPU = txtCongnghecpu.Text;
        ttsp.LoaiCPU = txtLoaicpu.Text;
        ttsp.DungLuongRAM = txtRam.Text;
        ttsp.DuongLuongDia = txtHDD.Text;
        ttsp.KichThuocMH = txtdisplay.Text;
        ttsp.CarDoHoa = txtvga.Text;
        ttsp.KetNoiMang = txtnetwork.Text;
        ttsp.TichHop = txttichhop.Text;
        ttsp.HeDieuHanh = txtos.Text;
        ttsp.TrongLuong = txtkichthuoc.Text;
        ttsp.ThongTinPin = txtpin.Text;
        ttsp.KhuyenMai = txtkhuyenmai.Text;
        ttsp.XuatXu = txtxuatxu.Text;
        ttsp.ThoiGianSD = txtthoigianbaohanh.Text;
        ttsp.KeyBoard = txtkeyboard.Text;
        busSP.ThemSP(sp);
        busSP.ThemTTSP(ttsp);
        Load_sanpham();
    }

    protected void gvSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSanPham.PageIndex = e.NewPageIndex;
        Load_sanpham();
    }

    protected void gvSanPham_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int masp = int.Parse(gvSanPham.DataKeys[e.NewSelectedIndex].Value.ToString());
        BUS_SanPham busSP = new BUS_SanPham();
        DataTable dt = busSP.LoadSP_masp(masp);
        foreach (DataRow row in dt.Rows)
        {            
            txtMaSP.Text = row["MaSP"].ToString();
            txtTenSP.Text = row["TenSP"].ToString();
            txtHinhAnh.Text = row["Anh"].ToString();
            txtGia.Text = row["Gia"].ToString();
            ddlThuonghieu.SelectedValue = row["IDThuongHieu"].ToString();
            ddlTinhtrang.SelectedValue = row["TinhTrang"].ToString();
            ddlBaohanh.SelectedValue = row["BaoHanh"].ToString();
            txtSoluong.Text = row["SoLuong"].ToString();
        }

        BUS_ThongTinSP busTT = new BUS_ThongTinSP();
        dt = busTT.LoadTTSP_masp(masp);
        foreach (DataRow row in dt.Rows)
        {
            txtMaTT.Text=row["IDThongTinSP"].ToString();
            txtCongnghecpu.Text=row["CongNgheCPU"].ToString();
            txtLoaicpu.Text=row["LoaiCPU"].ToString();
            txtRam.Text = row["DungLuongRam"].ToString();
            txtHDD.Text = row["DungLuongODia"].ToString();
            txtdisplay.Text = row["KichThuocMH"].ToString();
            txtvga.Text = row["CardDoHoa"].ToString();
            txtnetwork.Text = row["KetNoiMang"].ToString();
            txttichhop.Text = row["TichHop"].ToString();
            txtos.Text = row["HeDieuHanh"].ToString();
            txtkichthuoc.Text = row["TrongLuong"].ToString();
            txtpin.Text = row["ThongTinPin"].ToString();
            txtkhuyenmai.Text = row["KhuyenMai"].ToString();
            txtxuatxu.Text = row["XuatXu"].ToString();
            txtthoigianbaohanh.Text = row["ThoiGianBH"].ToString();
            txtkeyboard.Text = row["KeyBoard"].ToString();
        }
        
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        BUS_SanPham busSP = new BUS_SanPham();
        BUS_ThongTinSP busTT = new BUS_ThongTinSP();
        DTO_SanPham sp = new DTO_SanPham();
        DTO_ThongTinSP ttsp = new DTO_ThongTinSP();
        sp.IDSanPham = int.Parse(txtMaSP.Text);
        sp.ThuongHieu = int.Parse(ddlThuonghieu.SelectedValue);
        sp.TenSanPham = txtTenSP.Text;
        sp.Anh = txtHinhAnh.Text;
        sp.Gia = Convert.ToDouble(txtGia.Text);
        sp.TinhTrang = ddlTinhtrang.SelectedValue;
        sp.ThoiGanBH = ddlBaohanh.SelectedValue;
        sp.SoLuong = int.Parse(txtSoluong.Text);
        ttsp.MaSP = int.Parse(txtMaSP.Text);
        ttsp.CongngheCPU = txtCongnghecpu.Text;
        ttsp.LoaiCPU = txtLoaicpu.Text;
        ttsp.DungLuongRAM = txtRam.Text;
        ttsp.DuongLuongDia = txtHDD.Text;
        ttsp.KichThuocMH = txtdisplay.Text;
        ttsp.CarDoHoa = txtvga.Text;
        ttsp.KetNoiMang = txtnetwork.Text;
        ttsp.TichHop = txttichhop.Text;
        ttsp.HeDieuHanh = txtos.Text;
        ttsp.TrongLuong = txtkichthuoc.Text;
        ttsp.ThongTinPin = txtpin.Text;
        ttsp.KhuyenMai = txtkhuyenmai.Text;
        ttsp.XuatXu = txtxuatxu.Text;
        ttsp.ThoiGianSD = txtthoigianbaohanh.Text;
        ttsp.KeyBoard = txtkeyboard.Text;        
        busSP.CapNhat_SP(sp);
        busTT.CapNhat_ThongTinSP(ttsp);
        Load_sanpham();
    }

    protected void btnUphinh_Click(object sender, EventArgs e)
    {
        if (FileUpload1.FileName == "")
        {
            lblThongBao.Text = "Chưa chọn ảnh tải lên";
        }
        else
        {
            //Tách tên file và đuôi hay phần mở rộng .gif,.jpg....
            string[] strTen = FileUpload1.FileName.Split('.');
            string tenMoRong = strTen[strTen.Length - 1].ToLower();
            if (tenMoRong == "jpg" || tenMoRong == "png" || tenMoRong == "gif")
            {
                string strDuongDanLienHe = "../Img_SanPham/" + FileUpload1.FileName;
                string strDuongDanTuyetDoi = Server.MapPath(strDuongDanLienHe);
                //upfile
                FileUpload1.SaveAs(strDuongDanTuyetDoi);
                txtHinhAnh.Text = "Img_SanPham/" + FileUpload1.FileName.ToString();
            }
            else
            {
                lblThongBao.Text = "File đã chọn không đúng định dạng";
            }
        }
    }

    private void load_nhomSP()
    {
        BUS_ThuongHieu busTH = new BUS_ThuongHieu();
        DataTable dt = new DataTable();
        dt = busTH.Load_Thuonghieu();
        ddlThuonghieu.DataSource = dt.DefaultView;
        ddlThuonghieu.DataTextField = "TenThuongHieu";
        ddlThuonghieu.DataValueField = "IDThuongHieu";
        ddlThuonghieu.DataBind();
    }
    private void Load_sanpham()
    {
        BUS_SanPham busSP = new BUS_SanPham();
        DataTable dt = new DataTable();
        dt = busSP.Load_SP();
        gvSanPham.DataSource = dt.DefaultView;
        gvSanPham.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

    }
}